package com.qf.flash.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.qf.dblib.dao.entity.GroupDetailBean;
import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.constant.Constant;
import com.qf.flash.constant.FlashCodes;
import com.qf.flash.http.response.GameDetailResponse;
import com.qf.flash.mvp.contract.GameDetailContract;
import com.qf.flash.mvp.presenter.GameDetailPresenter;
import com.qf.flash.ui.login.HomeActivity;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.widget.ViewUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static com.qf.flash.utils.MyUtil.goGroupChat;

/**
 * 2020/2/27 10:34
 * 支付结果
 */
public class OrderPaySuccessActivity extends BaseActivity implements GameDetailContract.View {
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.tv_game_no)
    TextView tvGameNo;
    @BindView(R.id.tv_game_name)
    TextView tvGameName;
    @BindView(R.id.tv_game_time)
    TextView tvGameTime;
    @BindView(R.id.tv_game_password)
    TextView tvGamePassword;
    @BindView(R.id.tv_game_type)
    TextView tvGameType;
    @BindView(R.id.tv_game_number)
    TextView tvGameNumber;
    @BindView(R.id.tv_script_name)
    TextView tvScriptName;
    @BindView(R.id.tv_go_chosen)
    TextView tv_go_chosen;
    private GameDetailPresenter mGameDetailPresenter;
    private String id;
    private String playId;
    private GameDetailResponse response;
    private String emchatId;
    private String teamName;
    private String playImage;

    /**
     * @param id 组队Id
     */
    public static void start(Activity activity, String id, boolean isSponsor) {
        Intent intent = new Intent(activity, OrderPaySuccessActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("isSponsor", isSponsor);
        activity.startActivity(intent);
    }

    @Override
    protected void initInject() {
        mGameDetailPresenter = new GameDetailPresenter();
        initPresenters(mGameDetailPresenter);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay_success;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("支付结果");
        ViewUtil.setRightIcon(tvTopRight, R.mipmap.home_icon);
        id = getIntent().getStringExtra("id");
        boolean isSponsor = getIntent().getBooleanExtra("isSponsor", false);
        if (isSponsor && !FlashCodes.USER_TYPE_SHOP.equals( UserMgr.getInstance().getLoginUser().getUserType())) {
            tv_go_chosen.setVisibility(View.VISIBLE);
        }
        mGameDetailPresenter.getGameDetail(id);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_gon_chat, R.id.tv_go_chosen,R.id.ll_top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.ll_top_right:
                HomeActivity.start(this, 0);
                break;
            case R.id.tv_gon_chat:
//                HomeActivity.start(this, 2);     //  进入群组聊天
                if (!TextUtils.isEmpty(emchatId))
                    goGroupChat(OrderPaySuccessActivity.this, Constant.CHATTYPE_GROUP, emchatId);
                break;
            case R.id.tv_go_chosen:
                if (this.response != null)
                    ChoseBusinessActivity.start(this, playId, this.response.getPlayType(), id, emchatId);
                break;
        }
    }


    @Override
    public void setGDetail(GameDetailResponse response) {
        if (response == null)
            return;
        this.response = response;
        //--设置群名--
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().groupManager().changeGroupName(response.getEmchatId(), response.getTeamName());//需异步处理  ---设置群聊名称
                    GroupDetailBean groupDetailBean = new GroupDetailBean();
                    groupDetailBean.setPlayImage(response.getPlayImage());
                    groupDetailBean.setId(response.getEmchatId());
                    groupDetailBean.setEmchatId(response.getEmchatId());
                    groupDetailBean.save();
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        tvGameNo.setText("队伍编号: " + response.getOrderNumber());
        tvGameName.setText("队伍名称: " + response.getTeamName());
        tvGameNumber.setText("参与人数: " + response.getNeedNum());
        tvGamePassword.setText("加入密码: " + response.getGamePassword());
        tvGameTime.setText("开始时间: " + response.getGameTime());
        tvScriptName.setText("剧本名称: " + response.getPlayName());
        tvGameType.setText("游戏题材: " + response.getTypeName());
        playId = response.getPlayId();
        emchatId = response.getEmchatId();   // 群聊id
        teamName = response.getTeamName();
        playImage = response.getPlayImage();//
    }


}
